﻿using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace Adams.Web.Data.DailyPrayers
{
    public static class Index
    {
        public static Adams.Web.Models.DailyPrayers.Index.PageLoad PageLoad(string location, int monthId, bool isHijri)
        {
            var data = new Adams.Web.Models.DailyPrayers.Index.PageLoad() { 
                Month = monthId,
                PrayerTimes = new Shared.Entities.DailyPrayers.PrayerTimeCollection(),
                Locations = new Shared.Entities.DailyPrayers.LocationCollection()
            };

            using (SqlDataReader dr = DB.RunDataReader(@"
DECLARE @LocationID int
DECLARE @InheritMaster bit

IF(@Location = '0')
BEGIN
    SELECT @LocationID = LocationID, @InheritMaster = 0 FROM DailyPrayers.Locations WHERE IsMaster = 1
END
ELSE
BEGIN
    SELECT @LocationID = LocationID, @InheritMaster = InheritMaster FROM DailyPrayers.Locations WHERE Name = @Location
END

SELECT LocationID, Name
FROM DailyPrayers.Locations
WHERE LocationID = @LocationID

IF(@InheritMaster = 1)
BEGIN
    
    IF (@IsHijri = 1)
    BEGIN
        SELECT A.*
        FROM DailyPrayers.PrayerTimes A
        WHERE LocationID = (SELECT LocationID FROM DailyPrayers.Locations WHERE IsMaster = 1) AND IMonth = @MonthID
        ORDER BY IMonth, IDay
    END
    ELSE
    BEGIN
        SELECT A.*
        FROM DailyPrayers.PrayerTimes A
        WHERE LocationID = (SELECT LocationID FROM DailyPrayers.Locations WHERE IsMaster = 1) AND Month = @MonthID
        ORDER BY Month, Day
    END
END
ELSE
BEGIN
    IF (@IsHijri = 1)
    BEGIN
        SELECT A.*
        FROM DailyPrayers.PrayerTimes A
        WHERE LocationID = @LocationID AND IMonth = @MonthID
        ORDER BY IMonth, IDay
    END
    ELSE
    BEGIN
        SELECT A.*
        FROM DailyPrayers.PrayerTimes A
        WHERE LocationID = @LocationID AND Month = @MonthID
        ORDER BY Month, Day
    END
END

SELECT * 
FROM DailyPrayers.Locations
ORDER BY DisplayOrder
", CommandType.Text,
    DB.CreateParameter("@Location", location),
    DB.CreateParameter("@MonthID", monthId),
    DB.CreateParameter("@IsHijri", isHijri)))
            {
                dr.Read();
                data.LocationID = dr["LocationID"].DbInt();
                data.LocationName = dr["Name"].DbString();

                dr.NextResult();
                while (dr.Read())
                {
                    var prayer = DB.InitFromReader<Shared.Entities.DailyPrayers.PrayerTime>(dr, new ColMap { NameInDb = "PrayerID", NameInObject = "ID" });
                    data.PrayerTimes.Add(prayer);
                }

                dr.NextResult();
                while (dr.Read())
                {
                    var loc = DB.InitFromReader<Shared.Entities.DailyPrayers.Location>(dr, new ColMap { NameInDb = "LocationID", NameInObject = "ID" });
                    data.Locations.Add(loc);
                }
                return data;
            }
        }
    }
}